package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.member.ShippingAddressEntity;
import com.dragon.modules.yuegou.mapper.member.ShippingAddressMapper;
import com.dragon.modules.yuegou.model.member.ShippingAddress;
import com.dragon.modules.yuegou.query.member.ShippingAddressQuery;
import com.dragon.modules.yuegou.service.member.ShippingAddressService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 收货地址管理 服务实现
 *
 * @author pbuilder generated
 * @date 2020-02-25 10:48:46
 * @version 2.0
 */
@Service("memShippingAddressService")
public class ShippingAddressServiceImpl
		extends ServiceImpl<ShippingAddressMapper, ShippingAddressEntity>
		implements ShippingAddressService {

	@Override
	public ShippingAddress getDefaultAddress(String memberId) {
		ShippingAddressQuery query = new ShippingAddressQuery();
		query.setMemberId(memberId);
		query.setAddressDefault(true);
		List<ShippingAddressEntity> list = list(query.toQueryWrapper());
		if (CollectionUtils.isEmpty(list)){
			return new ShippingAddress();
		}
		return new ShippingAddress(list.get(0));
	}

	@Override
	public void setDefaultAddress(String memberId) {
		ShippingAddressQuery query = new ShippingAddressQuery();
		query.setMemberId(memberId);
		List<ShippingAddressEntity> list = list(query.toQueryWrapper());
		if (!CollectionUtils.isEmpty(list) && list.size() == 1) {
			ShippingAddressEntity entity = list.get(0);
			entity.setAddressDefault(true);
			baseMapper.updateById(list.get(0));
		}
	}
}